package whf.shop.entity;

import java.util.Iterator;
import java.util.List;

import whf.framework.util.Pair;
import whf.framework.util.Utils;

public class Order extends whf.framework.entity.AbstractEntity{

	private static final long serialVersionUID = 0;

	private String status;

	private double productCost;

	private double shippingCost;

	private double discount;

	private java.sql.Timestamp orderDate;

	private java.sql.Timestamp shipDate;

	private whf.shop.entity.Member member;

	private String giftNote;

	private whf.shop.entity.Currency currency;

	private double exchangeRate;

	private whf.shop.entity.Delivery delivery;

	private whf.shop.entity.Shop shop;

	private List<OrderItem> items = Utils.newArrayList();;

	// ---------- methods ----------

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return this.status;
	}

	public void setProductCost(double productCost) {
		this.productCost = productCost;
	}

	public double getProductCost() {
		return this.productCost;
	}

	public void setShippingCost(double shippingCost) {
		this.shippingCost = shippingCost;
	}

	public double getShippingCost() {
		return this.shippingCost;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public double getDiscount() {
		return this.discount;
	}

	public void setOrderDate(java.sql.Timestamp date) {
		this.orderDate = date;
	}

	public java.sql.Timestamp getOrderDate() {
		return this.orderDate;
	}

	public void setShipDate(java.sql.Timestamp shipDate) {
		this.shipDate = shipDate;
	}

	public java.sql.Timestamp getShipDate() {
		return this.shipDate;
	}

	public void setMember(whf.shop.entity.Member member) {
		this.member = member;
	}

	public whf.shop.entity.Member getMember() {
		return this.member;
	}

	public void setGiftNote(String giftNote) {
		this.giftNote = giftNote;
	}

	public String getGiftNote() {
		return this.giftNote;
	}

	public void setCurrency(whf.shop.entity.Currency currency) {
		this.currency = currency;
	}

	public whf.shop.entity.Currency getCurrency() {
		return this.currency;
	}

	public void setExchangeRate(double exchangeRate) {
		this.exchangeRate = exchangeRate;
	}

	public double getExchangeRate() {
		return this.exchangeRate;
	}

	public void setDelivery(whf.shop.entity.Delivery delivery) {
		this.delivery = delivery;
	}

	public whf.shop.entity.Delivery getDelivery() {
		return this.delivery;
	}

	public void setShop(whf.shop.entity.Shop shop) {
		this.shop = shop;
	}

	public whf.shop.entity.Shop getShop() {
		return this.shop;
	}

	/**
	 * @return Returns the items.
	 */
	public List<OrderItem> getItems() {
		return items;
	}

	/**
	 * @param items
	 *            The items to set.
	 */
	public void setItems(List<OrderItem> items) {
		this.items = items;
	}

	/**
	 * 向订单中增加订单条目
	 * 
	 * @modify wanghaifeng Jan 12, 2007 11:46:45 AM
	 * @param item
	 */
	public void addOrderItem(OrderItem item) {
		for (Iterator it = this.items.iterator(); it.hasNext();) {
			OrderItem oi = (OrderItem) it.next();
			if (oi.getProduct().getId() == item.getProduct().getId()) {
				oi.setQuantity(oi.getQuantity() + item.getQuantity());
				return;
			}
		}
		this.items.add(item);
	}

	/**
	 * 向订单中增加产品和数量,如果数量为0则从订单中移出
	 * 
	 * @modify wanghaifeng Jan 12, 2007 11:46:25 AM
	 * @param product
	 * @param quantity
	 */
	public void addProduct(Product product, int quantity) {
		if (product == null)
			return;

		for (Iterator it = this.items.iterator(); it.hasNext();) {
			OrderItem oi = (OrderItem) it.next();
			if (oi.getProduct().getId() == product.getId()) {
				oi.setQuantity(oi.getQuantity() + quantity);
				if (oi.getQuantity() == 0) {
					it.remove();
				}
				return;
			}
		}
		if (quantity > 0) {
			OrderItem oi = new OrderItem(product, quantity);
			this.items.add(oi);
		}
	}

	public void addProducts(List<Pair<Product, Integer>> products) {
		if (products == null || products.isEmpty())
			return;
		for (Pair<Product, Integer> pair : products) {
			if (pair.getValue() == null)
				continue;
			Product product = pair.getText();
			int quantity = pair.getValue().intValue();
			boolean found = false;
			;
			for (Iterator it = this.items.iterator(); it.hasNext();) {
				OrderItem oi = (OrderItem) it.next();
				if (oi.getProduct().getId() == product.getId()) {
					oi.setQuantity(oi.getQuantity() + quantity);
					if (oi.getQuantity() == 0) {
						it.remove();
					}
					found = true;
				}
			}
			if (!found) {
				this.items.add(new OrderItem(product, quantity));
			}
		}
	}

	/**
	 * 重新设置各产品数量，如果设置后为0，从订单中移出
	 * 
	 * @modify wanghaifeng Mar 31, 2007 1:34:54 PM
	 * @param products
	 */
	public void resetProductQuantity(List<Pair<Product, Integer>> products) {
		if (products == null || products.isEmpty())
			return;
		for (Pair<Product, Integer> pair : products) {
			if (pair.getValue() == null)
				continue;
			Product product = pair.getText();
			int quantity = pair.getValue().intValue();
			boolean found = false;
			;
			for (Iterator it = this.items.iterator(); it.hasNext();) {
				OrderItem oi = (OrderItem) it.next();
				if (oi.getProduct().getId() == product.getId()) {
					oi.setQuantity(quantity);
					if (oi.getQuantity() == 0) {
						it.remove();
					}
					found = true;
				}
			}
			if (!found) {
				this.items.add(new OrderItem(product, quantity));
			}
		}
	}

	public void removeOrderItem(Product product) {
		for (Iterator it = this.items.iterator(); it.hasNext();) {
			OrderItem oi = (OrderItem) it.next();
			if (product.equals(oi.getProduct())) {
				it.remove();
			}
		}
	}

	public int getProductCount() {
		int result = 0;
		for (Iterator it = this.items.iterator(); it.hasNext();) {
			OrderItem oi = (OrderItem) it.next();
			result += oi.getQuantity();
		}
		return result;
	}

	/**
	 * 不计任何折扣
	 * 
	 * @modify wanghaifeng Mar 31, 2007 5:32:14 PM
	 * @return
	 */
	public double getTotalCost() {
		double result = 0;
		for (Iterator it = this.items.iterator(); it.hasNext();) {
			OrderItem oi = (OrderItem) it.next();
			result += oi.getQuantity() * oi.getProduct().getPrice();
		}
		return result;
	}
}